package dp.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

public class DateUtil {
    public static final String C_TIME_PATTON_DEFAULT = "yyyy-MM-dd HH:mm:ss";
    public static final String C_DATE_PATTON_DEFAULT = "yyyy-MM-dd";
    public static final String YYYYMMDD = "yyyyMMdd";
    public static final String HHMMSS = "HH:mm:ss";
    
    public static final long SECONDS_IN_DAY = 1000 * 3600 * 24;

    public static void main(String[] args) {
        System.out.println(getTodayAsStr());
        System.out.println(getNowAsStr());
        System.out.println(getNowAsDate());
        System.out.println(getNowAsTimestamp());
        System.out.println(formatDate(getNowAsDate()));
        System.out.println(formatDate(getNowAsTimestamp()));
        System.out.println(formatTime(getNowAsDate()));
        System.out.println(formatTime(getNowAsTimestamp()));
        
        System.out.println("*****************");
        
        System.out.println(parse("2012-10-17"));
        System.out.println(parse2Timestamp("2012-10-17"));
        
        Date d1 = getNowAsDate();
        ThreadUtil.sleep(1000);
        Date d2 = getNowAsDate();
        System.out.println(DateUtil.daysBetween(d1, d2));
        System.out.println("*****************");
        System.out.println(firstDayOfMonth());
        System.out.println(lastDayOfMonth());
        
        //获取本周一的日期
        Calendar cal =Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); 
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        System.out.println(formatTime(cal.getTime()));
        
        
        System.out.println(DateUtil.formatDate(DateUtil.addDays(new Date(), 30)));
        System.out.println(DateUtil.formatDate(DateUtil.addDays(new Date(), -1)));
        System.out.println(DateUtil.formatDate(DateUtil.addDays(new Date(), -10)));
        System.out.println(DateUtil.formatDate(DateUtil.addDays(new Date(), -30)));
    }

    /**
     * 获得当前日期，格式yyyy-MM-dd
     * 
     * @return
     */
    public static String getTodayAsStr() {
        return getCurrentDate(C_DATE_PATTON_DEFAULT);
    }

    /**
     * 获得当前时间，格式yyyy-MM-dd hh:mm:ss
     * @return
     */
    public static String getNowAsStr() {
        return getCurrentDate(C_TIME_PATTON_DEFAULT);
    }
    
    /**
     * 取当前时间
     * @return
     */
    public static Date getNowAsDate(){
        return new Date();
    }
    
    /**
     * 取当前时间
     * @return
     */
    public static Timestamp getNowAsTimestamp(){
        return new Timestamp(System.currentTimeMillis());
    }

    /**
     * 获得当前时间，格式自定义
     * 
     * @param format
     * @return
     */
    public static String getCurrentDate(String format) {
        Calendar day = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String date = sdf.format(day.getTime());
        return date;
    }
    
    /**
     * 取当月的第一天
     * @return
     */
    public static String firstDayOfMonth(){
        return format(new Date(), "yyyy-MM-01");
    }
    
    /**
     * 取当月的最后一天
     * @return
     */
    public static String lastDayOfMonth(){
        Calendar cal = Calendar.getInstance();    
        int maxDay=cal.getActualMaximum(Calendar.DAY_OF_MONTH);    
        return format(new Date(), "yyyy-MM-"+maxDay);
    }

    /**
     * 将字符串转换成Date 格式可以是以下几种：yyyy-MM-dd；yyyy-MM-dd HH:mm:ss； yyyyMMdd
     * HH:mm:ss；yyyyMMdd；HH:mm:ss
     * @param strDate 字符串
     * @return date
     */
    public static Date parse(String strDate) {
        if (StringUtils.isBlank(strDate)){
            return null;
        }

        String pattern = C_DATE_PATTON_DEFAULT;
        if (strDate.indexOf("-") > 0 && strDate.indexOf(":") > 0) {
            pattern = C_TIME_PATTON_DEFAULT;
        } else if (strDate.indexOf("-") < 0 && strDate.indexOf(" ") > 0
                && strDate.indexOf(":") > 0) {
            pattern = "yyyyMMdd HH:mm:ss";
        } else if (strDate.indexOf("-") > 0 && strDate.indexOf(":") < 0) {
            pattern = C_DATE_PATTON_DEFAULT;
        } else if (strDate.indexOf("-") < 0 && strDate.indexOf(":") < 0) {
            pattern = YYYYMMDD;
        } else if (strDate.indexOf("-") < 0 && strDate.indexOf(":") > 0) {
            pattern = HHMMSS;
        }

        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        Date newDate = null;
        try {
            newDate = dateFormat.parse(strDate);
        } catch (ParseException pe) {
            newDate = null;
        }
        return newDate;
    }

    /**
     * 将字符串转换成Timestamp
     * @param dateStr 格式化的字符串
     * @return Timestamp
     */
    public static Timestamp parse2Timestamp(String dateStr) {
        Date date = parse(dateStr);
        if(date==null){
            return null;
        }
        else{
            return new Timestamp(date.getTime());
        }
    }

    /**
     * 将date格式化成字符串，格式2012-09-12
     * @param date 需要格式化的日期
     * @return 字符串，格式yyyy-MM-dd
     */
    public static String formatDate(Date date) {
        return format(date, C_DATE_PATTON_DEFAULT);
    }

    /**
     * 将date格式化成字符串，格式2012-09-12 14:24:23
     * @param date 需要格式化的日期
     * @return 字符串，格式yyyy-MM-dd HH:mm:ss
     */
    public static String formatTime(Date date) {
        return format(date, C_TIME_PATTON_DEFAULT);
    }

    /**
     * 将Date转化成指定格式的字符串
     * @param date 需要格式化的日期
     * @param pattern 格式
     * @return 字符串
     */
    public static String format(Date date, String pattern) {
        if (date == null || pattern == null){
            return null;
        }

        SimpleDateFormat dateFromat = new SimpleDateFormat();
        dateFromat.applyPattern(pattern);

        return dateFromat.format(date);
    }

    /**
     * 取得指定日期N天后的日期
     * @param date 指定日期
     * @param days 增加的天数，负数表示减
     * @return date
     */
    public static Date addDays(Date date, int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
//        cal.add(Calendar.DAY_OF_MONTH, days);
        cal.add(Calendar.DATE, days);
        return cal.getTime();
    }

    /**
     * 计算两个日期之间相差的天数
     * @param date1 date1
     * @param date2 date2
     * @return int 相差的天数
     */
    public static int daysBetween(Date date1, Date date2) {
        long between_days = (date2.getTime() - date1.getTime()) /SECONDS_IN_DAY;
        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 判断字符串是否符合时间格式
     * @param dateStr 字符串
     * @return true or false
     */
    public static boolean isDate(String dateStr) {
        if (StringUtils.isBlank(dateStr)) {
            return false;
        } else if (parse(dateStr) != null) {
            return true;
        } else {
            return false;
        }
    }

}
